package com.samebuilding.service.impl;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import com.samebuilding.dao.LeaveMessageDAO;
import com.samebuilding.dao.LeaveMessageReplyDAO;
import com.samebuilding.domain.LeaveMessage;
import com.samebuilding.domain.LeaveMessageReply;
import com.samebuilding.service.LeaveMessageService;

public class LeaveMessageServiceImpl implements LeaveMessageService {

	private LeaveMessageDAO leaveMessageDAO;
	private LeaveMessageReplyDAO leaveMessageReplyDAO;
	
	
	public LeaveMessageDAO getLeaveMessageDAO() {
		return leaveMessageDAO;
	}

	public void setLeaveMessageDAO(LeaveMessageDAO leaveMessageDAO) {
		this.leaveMessageDAO = leaveMessageDAO;
	}

	public LeaveMessageReplyDAO getLeaveMessageReplyDAO() {
		return leaveMessageReplyDAO;
	}

	public void setLeaveMessageReplyDAO(LeaveMessageReplyDAO leaveMessageReplyDAO) {
		this.leaveMessageReplyDAO = leaveMessageReplyDAO;
	}

	
	@Transactional
	public void addLeaveMessage(LeaveMessage leaveMessage) {
		this.leaveMessageDAO.save(leaveMessage);

	}

	@Transactional
	public void addLeaveMessageReply(LeaveMessageReply leaveMessageReply) {
		this.leaveMessageReplyDAO.save(leaveMessageReply);

	}

	@Transactional
	public void deleteByIdAndLeaveUserNo(Integer id, Integer leaveUserNo) {
		this.leaveMessageReplyDAO.deleteLeaveMessageReplyByLeaveMessageId(id);
		this.leaveMessageDAO.deleteByIdAndLeaveUserNo(id, leaveUserNo);
		

	}

	@Transactional
	public void deleteByIdAndReceiveUserNo(Integer id, Integer receiveUserNo) {
		this.leaveMessageReplyDAO.deleteLeaveMessageReplyByLeaveMessageId(id);
		this.leaveMessageDAO.deleteByIdAndReceiveUserNo(id, receiveUserNo);
		

	}

	
	public int getCountLeaveMessage(Integer leaveUserNo) {
		
		return this.leaveMessageDAO.getCountLeaveMessage(leaveUserNo);
	}

	public int getCountReceiveMessage(Integer receiveUserNo) {
		
		return this.leaveMessageDAO.getCountReceiveMessage(receiveUserNo);
	}

	public int getCountUnreadLeaveMessageReply(Integer leaveUserNo) {
		
		return this.leaveMessageReplyDAO.getCountUnreadLeaveMessageReply(leaveUserNo);
	}

	public List<LeaveMessage> getLeaveMessage(Integer leaveUserNo, int start,
			int limit) {
		
		return this.leaveMessageDAO.getLeaveMessage(leaveUserNo, start, limit);
	}

	public List<LeaveMessage> getReceiveMessage(Integer receiveUserNo,
			int start, int limit) {
		return this.leaveMessageDAO.getReceiveMessage(receiveUserNo, start, limit);
	}

	@Transactional
	public void updateLeaveMessageReplyUnreadToRead(Integer leaveUserNo) {
		this.leaveMessageReplyDAO.updateLeaveMessageReplyUnreadToRead(leaveUserNo);

	}

	@Transactional
	public void updateUnreadToRead(Integer receiveUserNo) {
		this.leaveMessageDAO.updateUnreadToRead(receiveUserNo);
		this.leaveMessageReplyDAO.updateLeaveMessageUnreadToRead(receiveUserNo);
	}

	
	@Transactional
	public void deleteById(Integer id) {
		this.leaveMessageReplyDAO.deleteLeaveMessageReplyByLeaveMessageId(id);
		this.leaveMessageDAO.delete(id);
		
	}

	public List<LeaveMessageReply> getReplyByLeaveMessageId(Integer leaveMessageId) {

		return this.leaveMessageReplyDAO.getReplyByLeaveMessageId(leaveMessageId);
	}

	public int getCountUnreadReceiveMessage(Integer receiveUserNo) {
		
		return this.leaveMessageDAO.getCountUnreadReceiveMessage(receiveUserNo) + this.leaveMessageReplyDAO.getCountUnreadLeaveMessage(receiveUserNo);
	}

	public List<LeaveMessage> findAll(int start, int limit) {
		
		return this.leaveMessageDAO.findAll(start, limit);
	}

	public int findCountAll() {
		
		return (int)this.leaveMessageDAO.getTotalCount();
	}

}
